package com.darrenchan.course07

import scala.math.ceil

/**
  * 匿名函数
  */
object FunctionApp extends App {
  val aaa = () => println("大驰哥")
  aaa()

  //柯里化函数
  def sum(a: Int, b: Int): Int = {
    a + b
  }

  def sum2(a: Int)(b: Int): Int = {
    a + b
  }

  println(sum(1, 2))
  println(sum2(1)(2))

  val l = List(1, 2, 3, 4, 5, 6, 7, 8)
  //map函数
  l.map((x: Int) => x * 2).foreach(x => print(x + " "))
  println()
  l.map((x) => x * 2).foreach(x => print(x + " "))
  println()
  l.map(x => x * 2).foreach(x => print(x + " "))
  println()
  l.map(_ * 2).foreach(x => print(x + " "))


  println()
  //filter函数
  l.map(_ * 2).filter(_ > 8).foreach(x => print(x + " "))

  //reduce
  //1+2 3+3 6+4 10+5
  l.reduce(_ + _)
  //0是初始值
  l.fold(0)(_ + _)

  val f = List(List(1, 2), List(3, 4), List(5, 6))
  f.flatten //List[Int] = List(1, 2, 3, 4, 5, 6)

  //flatmap相当于map取flatten
  f.flatMap(_.map(_*2)) //List[Int] = List(2, 4, 6, 8, 10, 12)

  val str = "hello,hello,world,hello"
  val ll = List(str)
  println()
  ll.flatMap(_.split(",")).map(x => (x, 1)).foreach(println)
  println()
  ll.flatMap(_.split(",")).map((_, 1)).foreach(println _)
  val func = ceil _

  println(func(3.14))
}
